<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          size="mini"
          label-width="118px"
        >
          <el-form-item label="主管海关" prop="mainPort">
            <el-select
              v-model.trim="queryParams.mainPort"
              placeholder="请选择主管海关"
              filterable
              clearable
            >
              <el-option v-for="(item, index) in customsList" :key="index" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="场所编码" prop="fieldCode">
            <el-select
              v-model.trim="queryParams.fieldCode"
              placeholder="请选择场所"
              filterable
              clearable
            >
              <el-option v-for="(item, index) in fieldOptions" :key="index" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="挂车牌号" prop="trailerLicenseNo">
            <el-input
              v-model.trim="queryParams.trailerLicenseNo"
              placeholder="请输入挂车牌号"
              clearable
            />
          </el-form-item>
          <el-form-item label="挂车注册地" prop="trailerRegPlace">
            <el-select
              v-model.trim="queryParams.trailerRegPlace"
              placeholder="请选择挂车注册地"
              filterable
              clearable
            >
              <el-option v-for="(item, index) in dict.type.sw_vehicle_reg_place" :key="index" :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="挂车颜色" prop="trailerColor">
            <el-input
              v-model.trim="queryParams.trailerColor"
              placeholder="请输入挂车颜色"
              clearable
            />
          </el-form-item>
          <el-form-item label="申报状态" prop="proposeStatus">
            <el-select
              v-model.trim="queryParams.proposeStatus"
              placeholder="请选择申报状态"
              clearable
            >
              <el-option
                v-for="dict in dict.type.sw_vehicle_decl_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="挂车备案类别"
            prop="trailerClassFlag"
          >
            <el-select v-model.trim="queryParams.trailerClassFlag" placeholder="请选择挂车备案类别">
              <el-option v-for="(item, index) in dict.type.road_trailer_type" :key="index"
                         :label="item.value+'-'+item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                            range-separator="-" start-placeholder="开始日期"
                            end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddEdit('add')"
            v-hasPermi="['bt:roadTrailer:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['bt:roadTrailer:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDecl"
            v-hasPermi="['bt:roadTrailer:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleAddEdit('editDecl')"
            v-hasPermi="['bt:roadTrailer:editDecl']"
          >变更申报
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['bt:roadTrailer:export']"
            >导出</el-button>
          </el-col>
          -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <!-- 关区代码 -->
        <template #mainPort="scope">
          <dict-value
            :list="customsList"
            :dictCode="scope.row.mainPort"
          ></dict-value>
        </template>
        <!-- 场所代码 -->
        <template #fieldCode="scope">
          <dict-value
            :list="fieldOptions"
            :dictCode="scope.row.fieldCode"
          ></dict-value>
        </template>
        <template #proposeStatus="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_decl_status"
            :value="scope.row.proposeStatus"
          ></dict-tag>
        </template>
        <template #responseCode="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_rcpt_status"
            :value="scope.row.responseCode"
          ></dict-tag>
        </template>
        <template #trailerColor="scope">
          <dict-tag
            :options="dict.type.motor_vehicles_color"
            :value="scope.row.trailerColor"
          ></dict-tag>
        </template>
        <template #trailerRegPlace="scope">
          <dict-tag
            :options="dict.type.sw_vehicle_reg_place"
            :value="scope.row.trailerRegPlace"
          ></dict-tag>
        </template>
        <template #trailerClassFlag="scope">
          <dict-tag
            :options="dict.type.road_trailer_type"
            :value="scope.row.trailerClassFlag"
          ></dict-tag>
        </template>
        <!-- <template #trailerLicenseNoPic="scope">
          <div v-if="scope.row.trailerLicenseNoPic != ''&&scope.row.trailerLicenseNoPic !=null">
            <a :href="baseUrl+scope.row.trailerLicenseNoPic" style="text-decoration: underline;" target="_blank">
              <el-button icon="el-icon-view" size="mini" type="primary">车牌照片</el-button>
            </a>
          </div>
          <div v-else>
            <el-button icon="el-icon-folder-delete" size="mini" type="danger">未上传照片</el-button>
          </div>
        </template>
        <template #veConPic="scope">
          <div v-if="scope.row.veConPic != ''&&scope.row.veConPic !=null">
            <a :href="baseUrl+scope.row.veConPic" style="text-decoration: underline;" target="_blank">
              <el-button icon="el-icon-view" size="mini" type="primary">车厢照片</el-button>
            </a>
          </div>
          <div v-else>
            <el-button icon="el-icon-folder-delete" size="mini" type="danger">未上传照片</el-button>
          </div>
        </template> -->
        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('edit', scope.row)"
              v-hasPermi="['bt:roadTrailer:edit']"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDelete(scope.row)"
              v-hasPermi="['bt:roadTrailer:remove']"
            >
              删除
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit
      ref="addEdit"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <detail-dialog
      ref="detail"
      :detailId="detailId"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      :dict="dictProp"
    ></detail-dialog>
  </div>
</template>

<script>
import {
  listRoadTrailer,
  delRoadTrailer,
  exportRoadTrailer,
  declRoadTrailer
} from "@/api/swdecl/roadTrailer";
import addEdit from "./components/addEdit";
import detailDialog from "./components/detailDialog";
import {getCustomsList} from "@/api/bt/bmhs";
import {findZcField} from "@/api/system/baseFieldInfo";

export default {
  name: "SwRoadTrailerRecord",
  components: {
    addEdit,
    detailDialog,
  },
  dicts: ["sw_vehicle_decl_status", "road_trailer_type", "sw_vehicle_reg_place", "sw_vehicle_rcpt_status","motor_vehicles_color"],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //上传地址
      baseUrl: process.env.VUE_APP_BASE_FILE_URL,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainPort: null,
        trailerLicenseNo: null,
        trailerRegPlace: null,
        trailerColor: null,
        fieldCode: null,
        proposeStatus: null,
        responseCode: null,
        responseType: null,
        trailerClassFlag: null,
        eportId: null,
        identifyIdNo: null,
        createTime: null,
      },
      // 遮罩层
      loading: true,
      // 列表数据
      tableData: [],
      //日期
      dateRange: [],
      columns: [
        {
          prop: "mainPort",
          label: "主管海关",
          align: "center",
          slot: true,
          minWidth: 110
        },
        {
          prop: "trailerLicenseNo",
          label: "挂车牌号",
          align: "center",
          minWidth: 120
        },
        {
          prop: "trailerRegPlace",
          label: "挂车注册地",
          align: "center",
          minWidth: 90,
          slot: true
        },
        {
          prop: "trailerWt",
          label: "挂车重量",
          align: "center",
        },
        {
          prop: "trailerColor",
          label: "挂车颜色",
          align: "center",
          slot: true
        },
        {
          prop: "fieldCode",
          label: "场所编码",
          align: "center",
          slot: true,
          minWidth: 160,
        },
        // {
        //   prop: "trailerLicenseNoPic",
        //   label: "挂车牌号图片",
        //   align: "center",
        //   slot: true,
        //   minWidth: 110
        // },
        // {
        //   prop: "veConPic",
        //   label: "车厢照片",
        //   align: "center",
        //   slot: true,
        //   minWidth: 110
        // },
        {
          prop: "proposeStatus",
          label: "申报状态",
          align: "center",
          slot: true,
          minWidth: 100,
          fixed: "right"
        },

        // {
        //   prop: "responseType",
        //   label: "回执类型",
        //   align: "center",
        // },
        {
          prop: "trailerClassFlag",
          label: "挂车备案类别",
          align: "center",
          minWidth: 120,
          slot: true
        },
        // {
        //   prop: "eportId",
        //   label: "电子口岸报文ID",
        //   align: "center",
        //   minWidth: 140
        // },

        // {
        //   prop: "operationType",
        //   label: "操作类型",
        //   align: "center",
        // },
        // {
        //   prop: "identifyIdNo",
        //   label: "识别卡ID号",
        //   align: "center",
        //   minWidth: 130
        // },
        {
          prop: "proposer",
          label: "申请人",
          align: "center",
        },
        {
          prop: "proposeTime",
          label: "申请时间",
          align: "center",
          minWidth: 140
        },
        // {
        //   prop: "responseTime",
        //   label: "回执时间",
        //   align: "center",
        // },
        {
          prop: "responseCode",
          label: "回执状态",
          align: "center",
          slot: true,
          minWidth: 120,
          fixed: "right"
        },
        // {
        //   prop: "responseText",
        //   label: "回执描述",
        //   align: "center",
        //   minWidth: 100
        // },
        {
          prop: "createBy",
          label: "创建人",
          align: "center",
        },
        {
          prop: "createTime",
          label: "创建时间",
          align: "center",
          width: 140
        },
        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 140,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      dialogType: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,

      // 互市挂车备案表格数据
      roadTrailerList: [],
      //海关关区信息列表
      customsList: [],
      // 场所列表
      fieldOptions: [],
      selectData: {},
    };
  },
  computed: {
    dictProp() {
      //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type,
      };
    },
  },
  created() {
    this.getList();
    this.loadCustomsList();
    this.getZcFieldInfo();
  },
  methods: {
    /** 查询互市挂车备案列表 */
    getList() {
      this.loading = true;
      listRoadTrailer(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      getCustomsList().then((response) => {
        this.customsList = response;
      });
    },
    // 场所列表
    getZcFieldInfo() {
      let obj = {fieldName: null, customCode: null};
      findZcField(obj).then((response) => {
        this.fieldOptions = response;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.pageParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleAddEdit(type, row) {
      this.dialogType = type;
      if (row == undefined || row == null || row == {}) {
        row = this.selectData;
      }
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      this.$refs.addEdit.open();
    },
    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectData = selection[0];
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除互市挂车备案编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRoadTrailer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    //申报
    handleDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要申报的备案数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declRoadTrailer(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有互市挂车备案数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportRoadTrailer(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {
        });
    },
  },
};
</script>


<style type="text/css">
.el-form-item .el-select {
  width: 100%;
}
</style>
